//
//  DMHomeTableViewCell.swift
//  DM_新浪微博
//
//  Created by Damon.JiAo on 16/3/1.
//  Copyright © 2016年 Demon_JiAo. All rights reserved.
//

import UIKit
import SnapKit


class DMHomeTableViewCell: UITableViewCell {
    /** 用来记录底部视图约束的变量 */
    var constant:Constraint?
    
    var statusCellViewModel:DMHomeViewCellModel?{
        /** 赋值之后会调用 */
        didSet{
            orginalView.statusViewModel = statusCellViewModel
            
            footerView.footerViewModel = statusCellViewModel
            
            /** 为了防止约束重复定义 -- 先清除约束 */
            constant?.uninstall()
            
            // 判断是否是转发微博
            if statusCellViewModel?.cellModel?.retweeted_status == nil{
             
                retweetView.hidden = true
                footerView.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.constant = make.top.equalTo(orginalView.snp_bottom).constraint
                    
                })
            }else{
                
                retweetView.hidden = false
                
                retweetView.retweetViewModel = statusCellViewModel
                
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constant = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            }
            
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI(){
        
        //  添加到视图里
        contentView.addSubview(orginalView)
        contentView.addSubview(footerView)
        contentView.addSubview(retweetView)
        
        //  添加约束
        orginalView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
//            make.bottom.equalTo(contentView.snp_bottom)
        }
        
        footerView.snp_makeConstraints { (make) -> Void in
            
            /** 记录一下当前footerView的约束 */
            self.constant = make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.left.equalTo(contentView.snp_left)
//            make.top.equalTo(orginalView.snp_bottom)
            make.right.equalTo(contentView.snp_right)
            make.bottom.equalTo(contentView.snp_bottom)
            
            //设置高度
            make.height.equalTo(40)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            
            // 注意
            make.top.equalTo(orginalView.snp_bottom)

        }
        
        //什么把视图,放值到contentView
        /*
        contentView是根据自动计算的左右上下
        一定注意   !!!!!!!!!!!!!!!!!!!!!!!  有个坑！！
        */
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(footerView.snp_bottom)
        }
        
    }
    
    private lazy var orginalView:DMOrginalView = DMOrginalView()
    
    private lazy var footerView:DMFooterView = DMFooterView()
    
    private lazy var retweetView:DMRetweetView = DMRetweetView()
    
}
